#ifndef TASKAGENT_H
#define TASKAGENT_H

#include <QObject>
#include <QTimer>
#include <QThread>
#include <QMutex>
#include <QSet>
#include "../app/safeQueue/readerwriterqueue.h"

#define TASKAGENT HIM::TaskAgent::getInstance()

namespace HIM {

class TaskAgentThread : public QThread
{
    Q_OBJECT
public:
    explicit TaskAgentThread(){};
    ~ TaskAgentThread(){};
    virtual void run() override;

public slots:
    void on_timeout();
};

class TaskAgent : public QObject
{
    Q_OBJECT
public:
    static TaskAgent& getInstance(){
        static TaskAgent instance;
        return instance;
    };

    void doTask();

    QSet<int> getSkipIndexs(){
        return skipIndexs;
    };

    void addSkipIndexs(int index){
        skipIndexs.insert(index);
    };

    void deleteSkipIndexs(int index){
        skipIndexs.remove(index);
    };

    void setLayerHigh(double value){
        layerHigh = value;
    }

    int checkPrintReady();

    void moveFeedServoAvoid();
    void moveManServoAvoid();
    void moveFeedSpreading();
    void moveManSpreading();
    void scraperToZero();
    void scraperCover();
    bool checkFeedInPlace();
    bool checkManInPlace();
    bool checkPowerInPlace();
    void slicelayerScan();
    void generateCurrentLayer();
    void printFinish();

    void startPrintTask();
    bool pausePrintTask();
    bool stopPrintTask();

    void setFirstPowderSpreaded(bool value){
        firstPowderSpreaded = value;
    }

    void setBaseHeating(bool value){
        baseHeating = value;
    }

    void setAdjPcMulValue(double value){
        adjPcMulValue = value;
    }

    void setAdjLaserPowerRatio(double value){
        adjLaserPowerRatio = value;
    }

    void setAdjLaserSpeedRatio(double value){
        adjLaserSpeedRatio = value;
    }

    double getAdjLaserPowerRatio(){
        return adjLaserPowerRatio;
    }

    double getAdjLaserSpeedRatio(){
        return adjLaserSpeedRatio;
    }

    void setAdjScraperSpeedRatio(double value){
        adjScraperSpeedRatio = value;
    }



    void setScrubbing(bool value){
        scrubbing = value;
    }

    bool getIsTaskWork(){
        return isTaskWork;
    }

private:
    TaskAgent();
    ~TaskAgent();
    TaskAgent(const TaskAgent&) = delete;
    TaskAgent& operator=(const TaskAgent&) = delete;

signals:
    void updataCliShow(int);
    void printFinished();

private:
    TaskAgentThread  *taskAgentThread;
    QSet<int> skipIndexs;

    bool firstPowderSpreaded{false};
    bool baseHeating{false};
    bool scrubbing{false};

    double layerHigh{0};

    double adjPcMulValue {1.};
    double adjLaserPowerRatio{1.0};
    double adjScraperSpeedRatio{1.0};
    double adjLaserSpeedRatio{1.0};

    std::atomic_bool threadPause{true};
    std::atomic_bool isTaskWork{false};
};
}
#endif // TASKAGENT_H
